package cn.yitun.chat.yitunserverimcore.general.main.biz.chat.manager;

import cn.yitun.chat.yitunservercommonlib.converter.dto2vo.user.UserLastChatDto2VoConverter;
import cn.yitun.chat.yitunservercommonlib.dataentity.dto.userchat.LastChatDto;
import cn.yitun.chat.yitunservercommonlib.dataentity.vo.userchat.LastChatVo;
import cn.yitun.chat.yitunserverimbase.dao.userchat.LastChatDao;
import com.github.pagehelper.PageHelper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 
 * Date 2019-03-08 09:12:44<br>
 * Description
 * 
 * @author yitun<br>
 * @since 1.1
 */
@Service
public class LastChatManager {

	@Resource
	private LastChatDao lastChatDAO;

	/**
	 * 保存
	 */
	public void saveOrUpdate(LastChatDto lastChat) {
		LastChatVo bean = lastChatDAO.getLastChat(lastChat.getUserId(), lastChat.getChatId(), lastChat.getType());
		if (null != bean) {
			bean.setMessageId(lastChat.getMessageId());
			bean.setContentId(lastChat.getContentId());
			lastChatDAO.update(UserLastChatDto2VoConverter.INSTANCE.vo2dto(bean) );
		} else {
			lastChatDAO.save(lastChat);
		}
	}

	/**
	 * 获取列表
	 */
	public List<LastChatVo> getList(Long userId, int pageNo, int pageSize) {
		return getList(userId, null, pageNo, pageSize);
	}

	/**
	 * 获取列表
	 */
	public List<LastChatVo> getList(Long userId,  String type, int pageNo, int pageSize) {
		PageHelper.startPage(pageNo, pageSize);
		return lastChatDAO.getUserLastChatList(userId, type);
	}
}
